$(document).ready(function(){
    var first_cate = $("#first_cate");
    var second_cate = $("#second_cate");
    var third_cate = $("#third_cate");
    
    
    var attr_area_outter = $("#attr-area-outter");
    var goods_publish_form = $("#goods-publish-form");
    var publish_submit_button = $("#publish-submit-button");
    var uploadimg_display_outter = $("#uploadimg-display-outter");
    var img_input_outter = $("#img-input-outter");
    
    
    var package_choose_line = $("#package-choose-line");
    var package_area_outter = $("#package-area-outter");
    var install_price_line = $("#install-price-line");
    var areanum_item = $("#areanum-line");
    var order_endtime_line_item = $("#order-endtime-line");
    var is_tall_line_item = $("#istall-line");
    var goodsprice_format = $("#goodsprice_format");
    var location_line = $("#location-line");
    
    function hide_useless_item(){
        install_price_line.val("");
        areanum_item.val("");
        order_endtime_line_item.val("");
        is_tall_line_item.val("");
        install_price_line.hide();
        areanum_item.hide();
        order_endtime_line_item.hide();
        is_tall_line_item.hide();
    }
    
//    if(first_cate.val()==1){
//           areanum_item.show();
//           order_endtime_line_item.show();
//           is_tall_line_item.show();
//           location_line.show();
//           package_choose_line.hide();
//           package_area_outter.hide();
//       }
//       
//       if(first_cate.val()==7){
//           hide_useless_item();
//           location_line.hide();
//       }
    
    var goods_attr_array = [];
    
    if($('#have_package').val() == 1){
        $('#package-area-outter').show();
    }
    
    var thirdCate = third_cate.val();
    $params = JSON.stringify({'cate_id':thirdCate});
    $cate_attr_back = ajaxSynchroRequest("/api/publish/get-attr",$params);
    goods_attr_array = $cate_attr_back.data;

    publish_submit_button.on('click',function(){
        var submit_able = 1;
        var data = {};
        //检测商品名称是否合法
        if($("#goodsname").val()==""){
            submit_able = 0;
            layer.msg('请输入商品名称！');
            return false;
        }
        //检测安装维护是否合法
        if($("#install_price").val()=="" && first_cate.val() == 1){
            submit_able = 0;
            layer.msg('请输入安装维护费！');
            return false;
        }
        //检测面积是否合法
        if($("#area_num").val()=="" && first_cate.val() == 1){
            submit_able = 0;
            layer.msg('请输入广告面积！');
            return false;
        }
        //检测价格
        if($("#price").val()==""){
            submit_able = 0;
            layer.msg('请输入商品价格！');
            return false;
        }
        
        //检测系统定义属性是否合法
        $.each(goods_attr_array,function(index,value){
            if(value.type==='0'){
                if(value.is_must==='1' && $("#attr_"+value.id).val()===""){
                    submit_able = 0;
                    layer.msg('【'+value.name+'】不能为空');
                    return false;
                }
                data['attr_'+value.id] = $("#attr_"+value.id).val();
            }
            if(value.type==='1'){
                if(value.is_must==='1' && $("#attr_"+value.id).val()===""){
                    submit_able = 0;
                    layer.msg('请选择【'+value.name+'】');
                    return false;
                }
                data['attr_'+value.id] = $("#attr_"+value.id).val();
            }
            if(value.type==='2'){
                if(value.is_must==='1' && $("[name = 'attr_"+value.id+"[]']:checked").length===0){
                    submit_able = 0;
                    layer.msg('至少选择一个【'+value.name+'】');
                    return false;
                }
                var checkbox_val = '';
                $.each($("[name = 'attr_"+value.id+"[]']:checked"),function(){
                    checkbox_val += ','+$(this).val();
                });
                checkbox_val = checkbox_val.substring(1);//去掉头部的逗号
                data['attr_'+value.id] = checkbox_val;
            }
        });
        //console.log(data);return false;
        
        //检测图片
        var uploadimg_display = $(".uploadimg-display");
        if(uploadimg_display.length == 0){
            submit_able = 0;
            layer.msg('至少要上传一张图片');
            return false;
        }
        
        img_input_outter.empty();
        for(var j=0;j<uploadimg_display.length;j++){
            //var img_input_html = '<input type="hidden" name="goodsimg_'+j+'" value="'+uploadimg_display[j].attributes[0].nodeValue+'"/>';
            data['goodsimg_'+j] = uploadimg_display[j].attributes[0].nodeValue;
        }
        
        var longitude =  $("#longitude").val();
        if(longitude == '' && (first_cate.val()==1 || first_cate.val()==8)){
            submit_able = 0;
            layer.msg('请选择位置');
            return false;
        }
        var latitude = $("#latitude").val();
        var region_area = $("#region_area").val();
        var address = $("#address").val();
        if(latitude == '' && (first_cate.val()==1 || first_cate.val()==8)){
            submit_able = 0;
            layer.msg('请选择位置');
            return false;
        }
        if(first_cate.val()==1 || first_cate.val()==8){
            data['longitude'] = longitude;
            data['latitude'] = latitude;
            data['region_area'] = region_area;
            data['address'] = address;
        }

        if(submit_able===1){
            data['goodsid'] = $("#goodsid").val();
            
            var have_package = $("#have_package").val();
            if(have_package == 1){
                var package = $("#package").val();
                data['package'] = package;
            }
            
            data['goodsname'] = $("#goodsname").val();
            data['price'] = $("#price").val();
            data['have_package'] = have_package;
            data['install_price'] = $("#install_price").val();
            data['area_num'] = $("#area_num").val();
            data['is_tall'] = $("#is_tall").val();
            data['order_endtime'] = $("#order_endtime").val();
            
            $params = JSON.stringify(data);
            console.log($params);
            $goods_save_back = ajaxSynchroPostRequest("/api/publish/updatesave",$params);
            console.log($goods_save_back);
            if($goods_save_back.success == 1){
                layer.msg($goods_save_back.msg);
                window.location.href='/user/goodslist';
            }else{
                layer.msg($goods_save_back.msg);
            }
        }
    });
    
    
    $("#uploadimg-display-outter").on('click','.uploadeimg-remove',function(){
        $(this).parent().parent().remove();
    });
    
    $('#have_package').on('change',function(){
        if($(this).val() == 0){
            $('#price').removeAttr("disabled");
            $('#package-area-outter').hide();
        }else{
            $('#price').val(0);
            $('#price').attr("disabled","disabled");
            $('#package-area-outter').show();
        }
    });
    
    $('#goodsimg-upload').on('change',function(){
        var inputFile = document.getElementById('goodsimg-upload');
        var old_nums = $(".uploadimg-display").length;
        if(inputFile.files.length+old_nums>6){
            layer.open({
                content: '最多能上传6张图片',
                skin: 'msg',
                time: 3000 //3秒后自动关闭
            });
            return false;
        }
        
        for(var i = 0; i<inputFile.files.length ;i++){
            var fr = new FileReader(); // 这个FileReader应该对应于每一个读取的文件都需要重新new一个
            var files = inputFile.files[i]; // files可以获取当前文件输入框中选择的所有文件，返回列表
            fr.readAsDataURL(files); // 读取的内容是加密以后的本地文件路径
            fr.onload = function(e){ // 数据读取完成时触发onload对应的响应函数
                $params = JSON.stringify({'imgdata':e.target.result});
                $cate_attr_back = ajaxSynchroPostRequest("/api/publish/goodsimgupload",$params);
                console.log($cate_attr_back);
                if($cate_attr_back.success==1){
                    uploadimg_display_outter.append('<div class="uploadimg-result-single"><img src="'+$cate_attr_back.url+'" data-url="'+$cate_attr_back.url+'" class="uploadimg-display"><div class="remove-outter"><span class="uploadeimg-remove">移除</span></div></div>');
                }
            }
        }
    });
});
